package org.luxor.html.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.luxor.commons.core.web.controller.BaseController;
import org.luxor.commons.core.web.data.R;
import org.luxor.commons.jdbc.metadata.PageQuery;
import org.luxor.html.entity.User;
import org.luxor.html.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author Mr.Yan  @date 2019/9/25$
 */
@Slf4j
@RestController
@Api("用户管理")
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    @ApiOperation("分页列表")
    @PostMapping("/list")
    public R<IPage<User>> list(@RequestBody PageQuery<HashMap> searchPage) {
        IPage<User> data = userService.page(searchPage.getPage(), searchPage.getSearch());
        return R.ok(data);
    }

    @GetMapping("/user/{id}")
    @ApiOperation("根据id获取用户记录")
    public R getUser(@PathVariable Integer id) {
        User user = userService.getById(id);
        return R.ok(user);
    }

    @DeleteMapping("/user/{id}")
    @ApiOperation("根据id删除用户记录")
    public R delUser(@PathVariable Integer id) {
        userService.removeById(id);
        return R.ok();
    }

    @PutMapping("/user/update")
    @ApiOperation("根据id修改用户记录")
    public R updateUser(@RequestBody User user) {
        userService.updateById(user);
        return R.ok();
    }

    @PostMapping("/user/add")
    @ApiOperation("新增用户记录")
    public R addUser(@RequestBody User user) {
        userService.save(user);
        return R.ok();
    }


}
